package com.ahu.qifei.common.common.advice;

import com.ahu.qifei.common.bean.ResultModel;
import com.ahu.qifei.common.common.excetpion.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <p>
 * @description 全局异常处理: 使用 @RestControllerAdvice + @ExceptionHandler 注解方式实现全
 * 局异常处理
 * </p>
 *
 * @author chenyue
 * @since 2020-07-24
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {




	/**
	 * 处理所有不可知的异常
	 * @Title
	 * @Description
	 * @param e
	 * @return
	 * @throws
	 */
    @ExceptionHandler({Exception.class})
    public ResultModel globalExceptionHandler(Exception e) {
		Integer code=500;
    	String msg = e.getMessage();

		if(e instanceof ApiException) {
			code = ((ApiException)e).getErrorCode();
		} else if(e instanceof NullPointerException) {
		    msg = "内部错误";
        }

		log.error("全局异常：code: {}, msg: {}, Exception: {}", code, msg, e);
		return ResultModel.fail(code, msg);
    }


}

